package com.mengtian_tech.tag;

import java.io.IOException;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.jeecgframework.core.util.StringUtil;

public class CommonSelect extends TagSupport{
	 
	/**
	 * 
	 */
	private static final long serialVersionUID = -4789355518491746206L;
	private String name;
	private String url;
	private String idField;
	private String textField;
	private String value;
	private String datatype;
	private String callback; //数据填充完毕后的回调
	private String dataAttrs;
	
	public int doStartTag() throws JspTagException {
		return EVAL_PAGE;
	}
	public int doEndTag() throws JspTagException {
		try {
			JspWriter out = this.pageContext.getOut();
			out.print(end().toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}
	public StringBuffer end() {
		StringBuffer sb = new StringBuffer();
		
		sb.append("<select id='"+name+"' name='"+name+"' class='commonSelect'"); 
		if(datatype != null){
			sb.append("datatype='"+datatype+"'" );
		}
		sb.append("></select>");
		sb.append("<script>");
		sb.append("function "+name+"select(params){");
		sb.append("$.ajax({type:'GET',datatype:'json',url:'"+url+"'+params,");
		sb.append("success:function(data){ ");
		sb.append("$('#"+name+"').html('');");
		sb.append(" data=$.parseJSON(data);");
		sb.append("for(var i=0;i<data.length;i++){");
		sb.append("var d = data[i]; var id = d."+idField+" ; var text = d."+textField+";");
		
		
		sb.append("var option='<option value=\"'+id+'\" ");
		//获取扩展属性
		if(StringUtil.isNotEmpty(dataAttrs)){
			String[] attrs = dataAttrs.split(",");
			for(String attr : attrs){
				sb.append(" data-"+attr+"=\"'+d."+attr+"+'\"");
			} 
			
		}
		sb.append(">'+text+'</option>'; ");
		sb.append("$('#"+name+"').append(option); ");
		sb.append("}");
		if(value != null){
			sb.append("$('#"+name+"').val('"+value+"'); "); 
		}
		
		if(callback != null){
			sb.append(callback+"();");
		}
		
		sb.append("}});");
		 
		sb.append("}");
		
		sb.append(name+"select('');");//调用方法
		
		sb.append("</script>");
		return sb;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getIdField() {
		return idField;
	}
	public void setIdField(String idField) {
		this.idField = idField;
	}
	public String getTextField() {
		return textField;
	}
	public void setTextField(String textField) {
		this.textField = textField;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String selectValue) {
		this.value = selectValue;
	}
	public String getDatatype() {
		return datatype;
	}
	public void setDatatype(String datatype) {
		this.datatype = datatype;
	}
	public String getCallback() {
		return callback;
	}
	public void setCallback(String callback) {
		this.callback = callback;
	}
	public String getDataAttrs() {
		return dataAttrs;
	}
	public void setDataAttrs(String dataAttrs) {
		this.dataAttrs = dataAttrs;
	}
	
	
	 
}
